package com.tz.movie.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.movie.domain.Movie;
import com.tz.movie.domain.MovieExample;
import com.tz.movie.domain.User;
import com.tz.movie.domain.UserExample;
import com.tz.movie.domain.UserMovie;
import com.tz.movie.domain.UserMovieExample;
import com.tz.movie.domain.status;
import com.tz.movie.mapper.MovieMapper;
import com.tz.movie.mapper.UserMapper;
import com.tz.movie.mapper.UserMovieMapper;

import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/usermovie")
public class BillController {
  @Autowired
  UserMovieMapper userMoviesMapper;

  @Autowired
  MovieMapper MovieMapper;
  @Autowired
  UserMapper UserMapper;

  @PostMapping(value = "/getAllUserMovie")
  public String getAllUserMovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    List<UserMovie> userMovies = userMoviesMapper.selectByExample(new UserMovieExample());
    JSONArray jsonArray = new JSONArray();
    for (UserMovie uMovies : userMovies) {
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("basedata", JSONObject.parse(uMovies.toString()));
      UserExample userExample = new UserExample();
      UserExample.Criteria criteria = userExample.createCriteria();
      criteria.andIdEqualTo(uMovies.getUid());
      List<User> user = UserMapper.selectByExample(userExample);
      if (user.size() > 0)
        jsonObject.put("userdata", JSONObject.parseObject(user.get(0).toString()));

      MovieExample movieExample = new MovieExample();
      MovieExample.Criteria criteria2 = movieExample.createCriteria();
      criteria2.andIdEqualTo(uMovies.getMid());
      List<Movie> movies = MovieMapper.selectByExample(movieExample);
      if (movies.size() > 0)
        jsonObject.put("moviedata", JSONObject.parseObject(movies.get(0).toString()));
      jsonArray.add(jsonObject);
    }
    result.put("result", status.s_ok);
    result.put("data", jsonArray);

    return result.toJSONString();
  }

  @PostMapping(value = "/getUserMovie")
  public String getUserMovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    int uid = jsonparm.getInteger("id");

    UserMovieExample uExample = new UserMovieExample();
    UserMovieExample.Criteria cc = uExample.createCriteria();
    cc.andUidEqualTo(uid);
    List<UserMovie> list = userMoviesMapper.selectByExample(uExample);
    JSONArray jsonArray = new JSONArray();
    for (UserMovie userMovie : list) {
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("basedata", JSONObject.parse(userMovie.toString()));
      UserExample userExample = new UserExample();
      UserExample.Criteria criteria = userExample.createCriteria();
      criteria.andIdEqualTo(userMovie.getUid());
      List<User> user = UserMapper.selectByExample(userExample);
      if (user.size() > 0)
        jsonObject.put("userdata", JSONObject.parseObject(user.get(0).toString()));

      MovieExample movieExample = new MovieExample();
      MovieExample.Criteria criteria2 = movieExample.createCriteria();
      criteria2.andIdEqualTo(userMovie.getMid());
      List<Movie> movies = MovieMapper.selectByExample(movieExample);
      if (movies.size() > 0)
        jsonObject.put("moviedata", JSONObject.parseObject(movies.get(0).toString()));
      jsonArray.add(jsonObject);
    }
    result.put("result", status.s_ok);
    result.put("data", jsonArray);
    return result.toJSONString();
  }

  @PostMapping(value = "/addUserMovie")
  public String addUserMovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    int uid = jsonparm.getInteger("uid");
    int mid = jsonparm.getInteger("mid");
    UserMovie uExample = new UserMovie();
    uExample.setUid(uid);
    uExample.setMid(mid);
    userMoviesMapper.insert(uExample);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

  @PostMapping(value = "/delUserMovie")
  public String delBill(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    int delid = jsonparm.getInteger("id");
    UserMovieExample usermove = new UserMovieExample();
    UserMovieExample.Criteria criteria = usermove.createCriteria();
    criteria.andIdEqualTo(delid);
    userMoviesMapper.deleteByExample(usermove);
    result.put("result", status.s_ok);

    return result.toJSONString();
  }
}